//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region nameSpaces
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using Microsoft.Research.ScientificWorkflow.TridentComposer;

    #endregion

    /// <summary>
    /// Interaction logic for ActivityBody.xaml
    /// </summary>
    public partial class ActivityBody : UserControl
    {
        #region Declaration

        /// <summary>
        /// Holds the activity model
        /// </summary>
        private SimpleActivityModel activityModel;

        private Dictionary<string, ActivityParameterDesigner> inputParamElements;

        private Dictionary<string, ActivityParameterDesigner> outputParamElements;
               
        #endregion

        #region Constructor

        /// <summary>
        /// Create the activityBody for the simple activity model.
        /// </summary>
        /// <param name="model">
        /// SimpleActivityModel.
        /// </param>
        /// <param name="paramElements">
        /// The parameter elements.
        /// </param>
        public ActivityBody(SimpleActivityModel model, Dictionary<string, ActivityParameterDesigner> inputParameterElements, Dictionary<string, ActivityParameterDesigner> outputParameterElements)
        {
            InitializeComponent();
            this.activityModel = model;
            this.Name = model.UniqueId;
            this.inputParamElements = inputParameterElements;
            this.outputParamElements = outputParameterElements;
            this.AddInputParameters();
            this.AddOutputParameters();
        }
        
        #endregion

        #region Properties

        /// <summary>
        /// get and sets the Border Height
        /// </summary>
        public double BodyHeight
        {
            get
            {
                return this.Height;
            }
            set
            {
                this.Height = value;
            }
        }

        /// <summary>
        /// get and sets the Border Width
        /// </summary>
        public double BodyWidth
        {
            get
            {
                return this.Width;
            }
            set
            {
                this.Width = value;
            }
        }

        /// <summary>
        /// get and sets the Body BackGround 
        /// </summary>
        public Brush BodyBackground
        {
            get
            {
                return this.brdrBodyHolder.Background;
            }
            set
            {
                this.brdrBodyHolder.Background = value;
            }
        }
                
        #endregion

        #region privateMethods

        /// <summary>
        /// Iterates ParameterDescriptionModel and add the Input parameter to Input stack
        /// </summary>
        private void AddInputParameters()
        {
            var inputParams = this.activityModel.InputParameters.ToList();
            inputParams.Sort(ParameterDescriptionModel.SortInputParameters);

            for (int counter = 0; counter < inputParams.Count; counter++)
            {
                ParameterDescriptionModel param = inputParams[counter] as ParameterDescriptionModel;
                if (param != null && this.inputParamElements.ContainsKey(param.PropertyName))
                {
                    ActivityInputParameterElement inputparameter = this.inputParamElements[param.PropertyName] as ActivityInputParameterElement;
                    if (inputparameter != null)
                    {
                        this.inputPanel.Children.Add(inputparameter);
                    }
                }
            }
        }

        /// <summary>
        /// Iterates ParameterDescriptionModel and add the Output parameter to Output stack
        /// </summary>
        private void AddOutputParameters()
        {
            for (int counter = 0; counter < this.activityModel.OutputParameters.Count; counter++)
            {
                ParameterDescriptionModel param = this.activityModel.OutputParameters[counter] as ParameterDescriptionModel;
                if (param != null && this.outputParamElements.ContainsKey(param.PropertyName))
                {
                    ActivityOutputParameterElement outputparameter = this.outputParamElements[param.PropertyName] as ActivityOutputParameterElement;
                    this.outputPanel.Children.Add(outputparameter);
                }
            }
        }

        #endregion

        #region public methods

        /// <summary>
        /// Bring the parameter outline into view.
        /// </summary>
        public void EnableParameterOutline(bool enableInput)
        {
            BrushConverter converter = new BrushConverter();
            Brush color = converter.ConvertFrom(UIDesignerCodeConstants.PARAMETERBOXCOLOR) as Brush;

            // Make the input or output outline enabled depending on the flag.
            if (enableInput)
            {
                this.inputParameterOutline.BorderBrush = color;
            }
            else
            {
                this.outputParameterOutline.BorderBrush = color;
            }
        }

        /// <summary>
        /// Make the parameter outline invisible.
        /// </summary>
        public void DisableParameterOutline(bool disableInput)
        {
            // Make the input or output outline disabled depending on the flag.
            if (disableInput)
            {
                this.inputParameterOutline.BorderBrush = Brushes.Transparent;
            }
            else
            {
                outputParameterOutline.BorderBrush = Brushes.Transparent;
            }
        }

        /// <summary>
        /// Get the position where the binding end point has to join the body.
        /// </summary>
        /// <returns>The point location</returns>
        public Point GetInputBorderConnectionPosition(UIElement relativeTo)
        {
            // Get the midpoint of the activity header.
            Point startPoint = new Point(0.0, this.inputParameterOutline.ActualHeight / 2.0);
            return this.inputParameterOutline.TranslatePoint(startPoint, relativeTo);
        }

        /// <summary>
        /// Get the position where the binding start point has to join the body.
        /// </summary>
        /// <returns>The point location.</returns>
        public Point GetOutputBorderConnectionPosition(UIElement relativeTo)
        {
            // Get the midpoint of the activity header.
            Point startPoint = new Point(this.outputParameterOutline.ActualWidth, this.outputParameterOutline.ActualHeight / 2.0);
            return this.outputParameterOutline.TranslatePoint(startPoint, relativeTo);
        }

        #endregion
    }
}
